package com.study.controller;

import com.study.dao.OrdersMapper;
import com.study.pojo.Book;
import com.study.pojo.User;
import com.study.service.BookService;
import com.study.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * 章节控制器
 * @Author Miss kun
 * @Date 2020/10/14 15:44
 * @Version 1.0
 */
@Controller
@RequestMapping("/chapter")
public class ChapterController {

    @Autowired
    private BookService bookService;
    @Autowired
    private OrdersService ordersService;

    /*提供给未登陆的用户的请求*/
    /**
     * 用来实现章节向前进行跳转
     * @param bookId
     * @param chapterId
     * @return
     */
    @RequestMapping("/sendChapter/delete/{bookId}/{chapterId}/{chapterTitle}")
    public String sendChapterDelete(@PathVariable Integer bookId, @PathVariable Integer chapterId,@PathVariable String chapterTitle, HttpSession session,Model model){
        System.out.println("sendChapter------>bookId:" + bookId + "章节id:" + chapterId + "章节标题:" + chapterTitle);

        User user = (User)session.getAttribute("user");
        Book bookChapter = null;

        if (user == null){
            //判断chapterId是否为第一章节，同时章节是否为左后一个id
            bookChapter = bookService.getChapterById(bookId, chapterId-1);
            if (bookChapter == null){
                bookChapter = bookService.getChapterById(bookId, chapterId);
                model.addAttribute("bookChapter",bookChapter);
                return "chapterText";
            }
            model.addAttribute("bookChapter",bookChapter);
            return "chapterText";
        }
        Integer userId = user.getId();
        System.out.println("用户id"+userId);
        //判断chapterId是否为第一章节，同时章节是否为左后一个id
        bookChapter = bookService.getChapterById(bookId, chapterId-1);
        if (bookChapter == null){
            bookChapter = bookService.getChapterById(bookId, chapterId);
            model.addAttribute("bookChapter",bookChapter);
            return "chapterText";
        }
        //跟新用户观看章节进度
        ordersService.updateChapterInfo(chapterTitle,chapterId,userId,bookId);
        model.addAttribute("bookChapter",bookChapter);
        return "chapterText";
    }

    /**
     * 实现章节向后跳转
     * @param bookId
     * @param chapterId
     * @param model
     * @return
     */
    @RequestMapping("/sendChapter/add/{bookId}/{chapterId}/{chapterTitle}")
    public String sendChapterAdd(@PathVariable Integer bookId, @PathVariable Integer chapterId, @PathVariable String chapterTitle , Model model, HttpSession session) {
        System.out.println("sendChapter------>bookId:" + bookId + "章节id:" + chapterId + "章节标题:" + chapterTitle);
        User user = (User) session.getAttribute("user");
        Book bookChapter = null;

        if (user == null) {
            //判断chapterId是否为第一章节，同时章节是否为左后一个id
            bookChapter = bookService.getChapterById(bookId, chapterId + 1);
            if (bookChapter == null) {
                bookChapter = bookService.getChapterById(bookId, chapterId);
                model.addAttribute("bookChapter", bookChapter);
                return "chapterText";
            }
            model.addAttribute("bookChapter", bookChapter);
            return "chapterText";
        }
        Integer userId = user.getId();
        System.out.println(userId);
        //判断chapterId是否为第一章节，同时章节是否为左后一个id
        bookChapter =  bookService.getChapterById(bookId, chapterId + 1);
        if (bookChapter == null){
            bookChapter = bookService.getChapterById(bookId, chapterId);
            model.addAttribute("bookChapter",bookChapter);
            return "chapterText";
        }
        //跟新用户观看章节进度
        ordersService.updateChapterInfo(chapterTitle,chapterId,userId,bookId);
        model.addAttribute("bookChapter",bookChapter);
        return "chapterText";
    }
}
